package server;

import command.Command;
import empty.users.User;

import java.io.*;
import java.net.Socket;

public class ServerThread extends Thread {
    private Socket socket;
    private OutputStream os;
    private InputStream is;
    private ObjectOutputStream oos;
    private ObjectInputStream ois;

    private User user;

    public ServerThread(Socket s){
        this.socket = s;
    }

    public ServerThread() {

    }

    @Override
    public void run() {
        try {
            os = this.socket.getOutputStream();
            is = this.socket.getInputStream();
            oos = new ObjectOutputStream(os);
            ois = new ObjectInputStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
        while (true){
            try {
                Command comm = (Command) ois.readObject();
                comm.excute();
                comm.excute(this);
                oos.writeObject(comm);
            } catch (Exception e) {
                System.out.println("客户端["+socket.getInetAddress()+"] 断开连接");
                try {
                    this.socket.close();
                    break;
                } catch (IOException ex) {
                    e.printStackTrace();
                }

            }
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


}
